/*
 * @Author: 我想吃余 483766615@qq.com
 * @Date: 2025-10-20 08:38:56
 * @LastEditors: 我想吃余 483766615@qq.com
 * @LastEditTime: 2025-10-21 09:16:23
 * @FilePath: /code/oscode/fifo/log.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once

#include <iostream>
#include <string>
#include <ctime>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

// 日志消息等级
#define Info 0
#define Warning 1
#define Error 2
#define Fatal 3
#define Debug 4

#define Screen 1
#define Onefile 2
#define Classfile 3

#define NUM 1024
#define LogFile "log.txt"

class Log
{
public:
    void Enable(int method)
    {
        printmethod = method;
    }

    char *LevelToString(int Level)
    {
        switch (Level)
        {
        case Info:
            return "Info";
        case Warning:
            return "Warning";
        case Error:
            return "Error";
        case Fatal:
            return "Fatal";
        case Debug:
            return "Debug";
        default:
            return "None";
        }
    }

    void OnefilePrint(const std::string &filename, const std::string &logtxt)
    {
        std::string _filename = path + filename; 
        int fd = open(_filename.c_str(), O_WRONLY|O_CREAT|O_APPEND, 0666);
        if(fd < 0) return;
        write(fd, logtxt.c_str(), logtxt.size());
        close(fd);
    }

    void ClassfilePrint(int Level, const std::string &logtxt)
    {
        std::string filename = LogFile;
        // 目标格式："log.txt.Debug/Warning/Fatal"
        filename += '.';
        filename += LevelToString(Level);
        OnefilePrint(filename, logtxt);
    }

    void PrintLog(int Level, const std::string &logtxt)
    {
        switch (printmethod)
        {
        case Screen:
            std::cout << logtxt << std::endl;
            break;
        case Onefile:
            OnefilePrint(LogFile, logtxt);
            break;
        case Classfile:
            ClassfilePrint(Level, logtxt);
            break;
        default:
            break;
        }
    }

    void operator()(int level, const char *format, ...)
    {
        // 1. 时间
        // 获取时间戳
        time_t t = time(nullptr);
        // 转为当地时间
        struct tm *ctime = localtime(&t);
        char leftbuffer[NUM];
        snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d:%d]", LevelToString(level), 
                                                ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday, 
                                                ctime->tm_hour, ctime->tm_min, ctime->tm_sec);

        // 2. 内容
        char rightbuffer[NUM];
        va_list s;
        va_start(s, format);
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, s);
        va_end(s);

        // 3. 合并left+right
        char logtxt[NUM * 2];
        snprintf(logtxt, sizeof(logtxt), "%s %s\n", leftbuffer, rightbuffer);

        PrintLog(level, logtxt);
    }

private:
    int printmethod = Screen;     //日志打印方式：默认打印到显示器文件
    std::string path = "./log/";  //创建日志的路径：默认在当前目录
};